package com.emp.test.springboottest.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.emp.test.springboottest.object.YhdOrdersGetParam;
import com.emp.test.springboottest.object.YhdProductAttributeGetParam;
import com.emp.test.springboottest.object.YhdProductStockGetParam;
import com.emp.test.springboottest.object.YhdRequestParam;
import com.emp.test.springboottest.object.YhdSearchProductsParam;
import com.emp.test.springboottest.object.YhdSysParam;
import com.emp.test.springboottest.service.YhdService;
import com.yhd.object.order.Order;
import com.yhd.object.product.PmStock;
import com.yhd.object.product.Product;
import com.yhd.response.basicAPI.BasicAreaGetResponse;
import com.yhd.response.product.GeneralProductsSearchResponse;
import com.yhd.response.product.ProductAttributeSingleGetResponse;
import com.yhd.response.product.ProductsStockGetResponse;

@RestController
@RequestMapping(value="/yhd")
public class YhdController {

	final static Logger logger = LoggerFactory.getLogger(YhdController.class);
	
	@Autowired
	YhdService yhdService;
	
	@RequestMapping("/getBasicArea")
	public BasicAreaGetResponse getBasicArea(){
		return yhdService.getBasicArea();
	}
	
	@RequestMapping("/order/getOrders/{sessionKey}")
	public List<Order> getOrders(@RequestParam(value = "orderStatusList") String orderStatusList, @RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime, @RequestParam("dateType") int dateType, @PathVariable String sessionKey) {
		logger.debug(orderStatusList +" " + startTime + " " + endTime + " " + dateType + " " + sessionKey);
		
		return yhdService.getOrders(new YhdRequestParam<YhdOrdersGetParam>(
				YhdSysParam.getBuilder().
				setSessionKey(sessionKey).
				build(),
				YhdOrdersGetParam.getBuilder()
				.setOrderStatusList(orderStatusList)
				.setStartTime(startTime)
				.setEndTime(endTime)
				.setDateType(dateType)
				.build()));
		
	}
	
	/**
	 * 用户搜索自己的产品信息，只支持普通产品
	 * @param sessionKey
	 * @param curPage
	 * @return
	 */
	@RequestMapping("/product/search/{sessionKey}")
	public GeneralProductsSearchResponse searchProducts(@PathVariable String sessionKey, @RequestParam(value="curPage", required=false, defaultValue = "1") int curPage ){
		logger.debug(sessionKey + " " + curPage);
		
		YhdSearchProductsParam appParam = new YhdSearchProductsParam();
		appParam.setCurPage(curPage);
		
		return yhdService.searchProducts(new YhdRequestParam<YhdSearchProductsParam>(
				YhdSysParam.getBuilder().
				setSessionKey(sessionKey).
				build(),
				appParam));
	}
	
	/**
	 * 获取单个商品的所有属性
	 * @param sessionKey
	 * @param productId
	 * @param outerId
	 * @return
	 */
	@RequestMapping("/product/attribute/{sessionKey}")
	public ProductAttributeSingleGetResponse getSingleProductAttribute(@PathVariable String sessionKey, @RequestParam(value = "productId", required = false) String productId, @RequestParam(value = "outerId", required=false) String outerId){
		logger.debug(sessionKey + " " + productId + outerId);
		Assert.isTrue(!(null == productId ||  productId.isEmpty()) && (null == outerId || outerId.isEmpty()),"productId 和 outerId 不能同时为空");
		
		YhdProductAttributeGetParam appParam = new YhdProductAttributeGetParam();
		appParam.setOuterId(outerId);
		appParam.setProductId(Long.valueOf(productId));
		
		return yhdService.getSingleProductAttribute(new YhdRequestParam<YhdProductAttributeGetParam>(
				YhdSysParam.getBuilder().
				setSessionKey(sessionKey).
				build(),
				appParam));
		
	}
	
	/**
	 * 获取商品制定某一个仓的库存信息
	 * @param sessionKey
	 * @param productIdList
	 * @param outerIdList
	 * @return
	 */
	@RequestMapping("/product/stocks/{sessionKey}")
	public ProductsStockGetResponse getStocks(@PathVariable String sessionKey,  @RequestParam(value = "productIdList", required=false) String productIdList, @RequestParam(value = "outerIdList", required=false) String outerIdList){
		logger.debug(sessionKey + " " + productIdList + " outerIdList : " + outerIdList) ;
		
		YhdProductStockGetParam appParam = new YhdProductStockGetParam();
		appParam.setOuterIdList(outerIdList);
		appParam.setProductIdList(productIdList);
		
		return yhdService.getStocks(new YhdRequestParam<YhdProductStockGetParam>(
				YhdSysParam.getBuilder().
				setSessionKey(sessionKey).
				build(),
				appParam));
		
	}
	
	
	/**
	 * 获取商品全部每一个仓库的库存信息
	 * @param sessionKey
	 * @param productIdList
	 * @param outerIdList
	 * @return
	 */
	@RequestMapping("/product/warehousestocks/{sessionKey}")
	public List<PmStock> getWarehouseStocks(@PathVariable String sessionKey,  @RequestParam(value = "productIdList", required=false) String productIdList, @RequestParam(value = "outerIdList", required=false) String outerIdList){
		logger.debug(sessionKey + " " + productIdList + " outerIdList : " + outerIdList) ;
		
		YhdProductStockGetParam appParam = new YhdProductStockGetParam();
		appParam.setOuterIdList(outerIdList);
		appParam.setProductIdList(productIdList);
		
		return yhdService.getWarehouseStocks(new YhdRequestParam<YhdProductStockGetParam>(
				YhdSysParam.getBuilder().
				setSessionKey(sessionKey).
				build(),
				appParam));
		
	}
	
	/**
	 * 同步全部产品信息
	 * @param sessionKey
	 * @return
	 */
	@RequestMapping("/product/update/{sessionKey}")
	public List<Product> updateAllProducts(@PathVariable String sessionKey){
		return yhdService.updateAllProducts(sessionKey);
	}
	
	/**
	 * 更新全部的普通商品的库存信息
	 * @param sessionKey
	 * @return
	 */
	@RequestMapping("/product/update/stock/{sessionKey}")
	public List<PmStock> updateAllProductStocks(@PathVariable String sessionKey){
		logger.debug(sessionKey) ;
		return yhdService.updateAllStocks(sessionKey);
	}
	
}
